from pydantic_settings import BaseSettings
from typing import Optional
import os
from dotenv import load_dotenv
from functools import lru_cache

load_dotenv()

class Settings(BaseSettings):
    # 数据库配置
    DATABASE_URL: str = "sqlite:///./dormitory.db"
    
    # JWT配置
    SECRET_KEY: str = "your-secret-key-here"
    ALGORITHM: str = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
    
    # 系统配置
    PROJECT_NAME: str = "宿舍管理和室友互选系统"
    VERSION: str = "1.0.0"
    API_V1_STR: str = "/api/v1"
    
    # 日志配置
    LOG_LEVEL: str = "INFO"
    JSON_LOGS: bool = False
    
    # 宿舍楼配置
    BUILDINGS_COUNT: int = 3
    FLOORS_PER_BUILDING: int = 6
    ROOMS_PER_FLOOR: int = 10
    BEDS_PER_ROOM: int = 4
    
    # 匹配算法配置
    MATCHING_WEIGHT_SCHEDULE: float = 0.4  # 作息时间权重
    MATCHING_WEIGHT_CLEANLINESS: float = 0.3  # 卫生习惯权重
    MATCHING_WEIGHT_INTERESTS: float = 0.3  # 兴趣爱好权重
    
    # 应用配置
    APP_NAME: str = "室友互选系统"
    DEBUG: bool = True
    
    # 匹配算法权重
    HABIT_WEIGHT: float = 0.3  # 作息习惯权重
    CLEANLINESS_WEIGHT: float = 0.3  # 卫生习惯权重
    INTEREST_WEIGHT: float = 0.4  # 兴趣爱好权重
    PREFERENCE_WEIGHT: float = 0.2  # 互选权重

    class Config:
        case_sensitive = True
        env_file = ".env"
        
        @classmethod
        def parse_env_var(cls, field_name: str, raw_val: str) -> any:
            if field_name == "JSON_LOGS":
                return raw_val.lower() in ("true", "1", "t", "y", "yes")
            return raw_val

@lru_cache()
def get_settings() -> Settings:
    """
    获取应用配置，使用lru_cache确保配置只被加载一次
    """
    return Settings()

# 导出配置实例
settings = get_settings() 